﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using VastPark.FrameworkBase.ComponentModel;
using System.IO;
using VastPark.Acquisition;

namespace ImmlPad.UpdateManagement
{
    /// <summary>
    /// Interaction logic for UpdateWindow.xaml
    /// </summary>
    public partial class UpdateWindow : Window
    {
        public event Event<string> DownloadCompleted;

        public Version Version
        {
            get { return (Version)GetValue(VersionProperty); }
            set { SetValue(VersionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Version.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VersionProperty =
            DependencyProperty.Register("Version", typeof(Version), typeof(UpdateWindow), new UIPropertyMetadata(new Version()));

        public float DownloadProgress
        {
            get { return (float)GetValue(DownloadProgressProperty); }
            set { SetValue(DownloadProgressProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DownloadProgress.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DownloadProgressProperty =
            DependencyProperty.Register("DownloadProgress", typeof(float), typeof(UpdateWindow), new UIPropertyMetadata(float.MinValue));

        public UpdateWindow()
        {
            InitializeComponent();
        }

        public void StartDownload(string uri)
        {
            //validate the uri
            uri = uri.Replace("&amp;", "&");

            var transferManager = new TransferManager();
            
            var resource = new UpdateResource(uri, new Action<float>(a => 
            {
                Dispatcher.Invoke((ThreadStart)delegate { this.DownloadProgress = a; });
            }));
            
            var activity = transferManager.Download(resource);

            activity.WhenDone(a =>
            {
                if (activity.HasError)
                {
                    return;
                }
                else
                {
                    var dir = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString());
                    Directory.CreateDirectory(dir);

                    var tmpPath = System.IO.Path.Combine(dir, "ImmlPad.msi");

                    using (var fs = new FileStream(tmpPath, FileMode.Create))
                    {
                        var bytes = new byte[a.Length];

                        a.Read(bytes, 0, bytes.Length);

                        fs.Write(bytes, 0, bytes.Length);
                    }

                    DownloadCompleted.Raise(this, tmpPath);
                }
            });
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
